package com.clei.utils.helper;

import java.util.HashMap;
import java.util.Map;

/**
 * MySQLDBHelper
 *
 * @author Y
 * @date 2024-01-30
 **/
public class TaosDBHelper extends DBHelper {

    /**
     * type map
     */
    private static final Map<String, String> TYPE_MAP = new HashMap<>(18);

    static {
        // 布尔
        TYPE_MAP.put("bool", "java.lang.Boolean");
        // 整数
        TYPE_MAP.put("tinyint", "java.lang.Integer");
        TYPE_MAP.put("tinyint unsigned", "java.lang.Integer");
        TYPE_MAP.put("smallint", "java.lang.Integer");
        TYPE_MAP.put("smallint unsigned", "java.lang.Integer");
        TYPE_MAP.put("int", "java.lang.Integer");
        TYPE_MAP.put("int unsigned", "java.lang.Long");
        TYPE_MAP.put("bigint", "java.lang.Long");
        TYPE_MAP.put("bigint unsigned", "java.lang.Long");
        // 字符串
        TYPE_MAP.put("binary", "java.lang.String");
        TYPE_MAP.put("nchar", "java.lang.String");
        TYPE_MAP.put("varchar", "java.lang.String");
        TYPE_MAP.put("json", "java.lang.String");
        TYPE_MAP.put("geometry", "java.lang.String");
        TYPE_MAP.put("varbinary", "java.lang.String");
        // 小数
        TYPE_MAP.put("float", "java.lang.Float");
        TYPE_MAP.put("double", "java.lang.Double");
        // 日期
        TYPE_MAP.put("timestamp", "java.lang.String");
    }

    /**
     * getJavaType
     *
     * @param type        columnType
     * @param defaultType defaultType
     * @return javaType
     */
    @Override
    String getJavaType(String type, String defaultType) {
        return TYPE_MAP.getOrDefault(type, defaultType);
    }

    /**
     * getString
     *
     * @param obj       obj
     * @param className className
     */
    @Override
    String getString(Object obj, String className) {
        String str;
        if (className.equals("byte[]")) {
            byte[] bytes = (byte[]) obj;
            str = new String(bytes);
        } else {
            str = String.valueOf(obj);
        }
        return str;
    }

    /**
     * getStringWithSingleQuotation
     *
     * @param obj       obj
     * @param className className
     */
    @Override
    String getStringWithSingleQuotation(Object obj, String className) {
        String str;
        switch (className) {
            case "Timestamp":
                str = '\'' + String.valueOf(obj) + '\'';
                break;
            case "byte[]":
                byte[] bytes = (byte[]) obj;
                str = '\'' + new String(bytes) + '\'';
                break;
            default:
                str = String.valueOf(obj);
        }
        return str;
    }
}
